#include "init.h"
#include "common.h"


//using namespace std;

//***************************************************************************//
//description: initilize NODE structre with a given topology matrix
//input: TopoMatrix[N][N+1], 
//       TopoMatrix[i][j], j=0,...,N-1: the conductance between node i and node j
//       TopoMatrix[i][j], j=N,N+1: inject/absorb current and voltage at node i
//output: NodesRecord: NODE structre, recorde information of nodes         
//***************************************************************************//
void topoInit (float **TopoMatrix, NODE* NodesRecord, UINT nodeNum)
{
      
       UINT i, j, k, branchNums = 0;
       float sumOfConductance;
       nbrNode neighborNode;
       
       for (i=0; i<nodeNum; i++)
       {
           NodesRecord[i].nodeID = i;
           
           if (*((float*)TopoMatrix+i*(nodeNum+2)+nodeNum+1)<VOLTAGECONSTANT)
           {
               NodesRecord[i].exactHomeFlag = 1;
           }
           else
           {
               NodesRecord[i].exactHomeFlag = 0;
           }
           NodesRecord[i].unexactHomeFlag = NodesRecord[i].exactHomeFlag; 
           NodesRecord[i].voltage = *((float*)TopoMatrix+i*(nodeNum+2)+nodeNum+1);
           NodesRecord[i].injectCurrent = *((float*)TopoMatrix+i*(nodeNum+2)+nodeNum);
           
           //the sum of conductance incident at node i
           sumOfConductance = 0;
           for (j=0; j<nodeNum; j++)    
           {
                sumOfConductance += *((float*)TopoMatrix+i*(nodeNum+2)+j);                     
           }
           
           NodesRecord[i].totalConductance = sumOfConductance;
           
           for (j=0; j<nodeNum; j++)    
           {    //check node's neighbors
                if (*((float*)TopoMatrix+i*(nodeNum+2)+j) >0)
                {     
                      branchNums++;
                      neighborNode.nodeID = j;
                      neighborNode.branchID = branchNums; 
                      neighborNode.conductance = *((float*)TopoMatrix+i*(nodeNum+2)+j);
                      neighborNode.injectCurrent = *((float*)TopoMatrix+j*(nodeNum+2)+nodeNum);
                      neighborNode.voltage =  *((float*)TopoMatrix+j*(nodeNum+2)+nodeNum+1);
                      neighborNode.homeFlag = (*((float*)TopoMatrix+j*(nodeNum+2)+nodeNum+1)<VOLTAGECONSTANT ? 1 :0); 
                      neighborNode.probability = *((float*)TopoMatrix+i*(nodeNum+2)+j)/sumOfConductance;
                      neighborNode.probability = neighborNode.probability * 100;
                      
                      //compute the sum Of Conductance at this neighbor nodes
                      neighborNode.totalConductance = 0;
                      for (k=0; k<nodeNum; k++)    
                      {
                          neighborNode.totalConductance += *((float*)TopoMatrix+j*(nodeNum+2)+k);                     
                      }
                      NodesRecord[i].neighborNodes.push_back(neighborNode);
                }
           }
       }      
}

